package com.atguigu.day07;

import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

public class Flink08_IntervalJoin {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        //第一条流
        SingleOutputStreamOperator<Tuple3<String, String, Long>> orderStream = env
                .fromElements(
                        Tuple3.of("Songsong", "order", 20 * 60 * 1000L)
                )
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<Tuple3<String, String, Long>>forMonotonousTimestamps()
                                .withTimestampAssigner(new SerializableTimestampAssigner<Tuple3<String, String, Long>>() {
                                    @Override
                                    public long extractTimestamp(Tuple3<String, String, Long> e, long l) {
                                        return e.f2;
                                    }
                                })
                );

        //第二条流
        SingleOutputStreamOperator<Tuple3<String, String, Long>> pvStream = env
                .fromElements(
                        Tuple3.of("Songsong", "pv", 15 * 60 * 1000L),
                        Tuple3.of("Songsong", "pv", 11 * 60 * 1000L),
                        Tuple3.of("Songsong", "pv", 9 * 60 * 1000L),
                        Tuple3.of("Songsong", "pv", 21 * 60 * 1000L),
                        Tuple3.of("Songsong", "pv", 25 * 60 * 1000L)
                )
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<Tuple3<String, String, Long>>forMonotonousTimestamps()
                                .withTimestampAssigner(new SerializableTimestampAssigner<Tuple3<String, String, Long>>() {
                                    @Override
                                    public long extractTimestamp(Tuple3<String, String, Long> e, long l) {
                                        return e.f2;
                                    }
                                })
                );

        //查看用户下订单前后5分钟做了什么
        orderStream
                .keyBy(new KeySelector<Tuple3<String, String, Long>, String>() {
                    @Override
                    public String getKey(Tuple3<String, String, Long> value) throws Exception {
                        return value.f0;
                    }
                })
                .intervalJoin(pvStream.keyBy(new KeySelector<Tuple3<String, String, Long>, String>() {
                    @Override
                    public String getKey(Tuple3<String, String, Long> value) throws Exception {
                        return value.f0;
                    }
                }))
                //默认包含上边界和下边界
                .between(Time.minutes(-5), Time.minutes(5))
                //设置不包含下边界
                .lowerBoundExclusive()
                //设置不包含上边界
                .upperBoundExclusive()
                .process(new ProcessJoinFunction<Tuple3<String, String, Long>, Tuple3<String, String, Long>, String>() {
                    @Override
                    public void processElement(Tuple3<String, String, Long> left, Tuple3<String, String, Long> right, Context ctx, Collector<String> out) throws Exception {
                        out.collect(Tuple3.of(left.f0, left.f1,left.f2/1000/60) + "->" + Tuple3.of(right.f0, right.f1, right.f2/1000/60));
                    }
                }).print();

        env.execute();

    }
}
